<?php
class setting extends top
{
	//重定义构造函数
    function __construct(){  
        parent::__construct();
        if($_SESSION['POWER']!=1 && $_SESSION['POWER']!=3){$this->jump(spUrl('main', 'index'));}
        //定义当前导航
        $this->nav_name = 'setting';
        $this->page_title = '设置';
        $this->breadcrumb += array('1'=>array('name'=>'设置'));
    }
    //构造左侧导航
    function __left_nav_common(){
		$left_nav_result = array(
				array(
						'id'=>1,
						'name'=>'网站设置',
						'url'=>spUrl('setting','webinfo'),
					),
				array(
						'id'=>2,
						'name'=>'编辑管理',
						'url'=>spUrl('setting','user'),
					),
			);
		$this->left_nav_result = $left_nav_result; 	
    }
	function index(){
		$this->webinfo();
		$this->display("tpl/setting_webinfo.html");
	}
	function webinfo(){
		$data = $this->spArgs();
		$this->__left_nav_common();
		$this->left_nav_id = 1;
		$this->breadcrumb += array('2'=>array('name'=>'网站设置'));
		//更新网站信息
		$notice = array($data['state'],$data['message']);
		$this->notice = $notice[0]?$notice:null;
		if($data['setting_update_submit']){
			if(1){
				$setting = spClass("m_setting");
				$is_ok_1 = $setting->update('id=1',array('content'=>$data['cms_title']));
				$is_ok_2 = $setting->update('id=2',array('content'=>$data['cms_description']));
				$is_ok_3 = $setting->update('id=3',array('content'=>$data['cms_address']));
				$is_ok_4 = $setting->update('id=4',array('content'=>$data['cms_postcode']));
				$is_ok_5 = $setting->update('id=5',array('content'=>$data['cms_phone']));
				$is_ok_6 = $setting->update('id=6',array('content'=>$data['cms_email']));
				$is_ok_7 = $setting->update('id=7',array('content'=>$data['cms_record']));
				$is_ok_8 = $setting->update('id=8',array('content'=>$data['cms_ann']));
				$is_ok_9 = $setting->update('id=9',array('content'=>$data['cms_is_time_visible']));

				$is_ok_10 = $setting->update('id=10',array('content'=>$data['email_server_address']));
				$is_ok_11 = $setting->update('id=11',array('content'=>$data['email_server_username']));
				$is_ok_12 = $setting->update('id=12',array('content'=>$data['email_server_password']));
				$is_ok_13 = $setting->update('id=13',array('content'=>$data['email_recieve_address']));
				$is_ok_14 = $setting->update('id=14',array('content'=>$data['web_shouye_info']));
				
				if($is_ok_1&&$is_ok_2&&$is_ok_3&&$is_ok_4&&$is_ok_5&&$is_ok_6&&$is_ok_7&&$is_ok_8){
					$this->jump(spUrl('setting', 'webinfo',array('state'=>'success','message'=>"更新成功！")));
				}else{
					$this->setting_return = $data;
					$this->notice = array('danger','更新出错！');
				}
			}else{
				$this->setting_return = $data;
				$this->notice = array('danger','填写有误！');
			}			
		}
	}
	function user(){
			$data = $this->spArgs();
			$this->__left_nav_common();
			$this->left_nav_id = 2;
			$this->breadcrumb += array('2'=>array('name'=>'编辑管理'));
			
			$user = spClass("m_user");
			//添加编辑
			if($data['user_add_submit']){
				if(1){
					$data_insert = array(
						'name'=>$data['name'],
						'password'=>md5($data['password'].'ytcms'),
						'power_grade'=>2,
					);
					if($user->create($data_insert)){
						$this->notice = array('success','添加成功！');
					}else{
						$this->notice = array('danger','添加出错！');
					}
				}else{
					$this->user_add_return = $data;
					$this->notice = array('danger','填写有误！');
				}		
			}
			//更新编辑信息
			if($data['user_update_submit']){
				if(1){
					$data_insert = array(
						'name'=>$data['name'],
						'password'=>md5($data['password'].'ytcms'),
					);
					if($user->update('id='.$data['id'],$data_insert)){
						$this->notice = array('success','更新成功！');
					}else{
						$this->notice = array('danger','更新出错！');
					}
				}else{
					$this->notice = array('danger','填写有误！');
				}			
			}
			//删除编辑
			if($data['user_delete_submit']){
				if($user->delete('id='.$data['id'])){
					$this->notice = array('success','删除成功！');
				}else{
					$this->notice = array('danger','删除出错！');
				}			
			}
			//更新管理员信息
			if($data['admin_update_submit']){
				$login_result = $user->find(array('name'=>$data['name1'],'power_grade'=>1));
				if($login_result){				
					if($login_result['password']==md5($data['password1']."ytcms")){
						if(1){
							$data_insert = array(
								'name'=>$data['name2'],
								'password'=>md5($data['password2'].'ytcms'),
							);
							if($user->update('id=1',$data_insert)){
								$_SESSION['USER'] = $data['name2'];
								$_SESSION['PASS'] = md5(md5($data['password2'].'ytcms').'check_session');
								$this->notice = array('success','更新成功！');
							}else{
								$this->notice = array('danger','更新出错！');
							}
						}else{
							$this->notice = array('danger','填写有误！');
						}					
					}else{
						//密码不正确
						$this->notice = array('danger','原密码不正确！');
					}
				}else{
						//用户名不存在
						$this->notice = array('danger','原用户名不存在！');
				}		
			}
			$this->user_result = $user->findAll('power_grade=2','','id,name,login_time');
	}
}